package name.fowler.refactoring.simplestore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import bonusstrategy.IBonusRecountStrategy;

public class Customer {
	private String customerName;
	private List<Rental> rentals = new ArrayList<Rental>();

	private IBonusRecountStrategy bonusStrategy;
	
	public Customer(String name, IBonusRecountStrategy bonusStrategy) {
		this.customerName = name;
		this.bonusStrategy = bonusStrategy;
	}

	public String getName() {
		return customerName;
	}

	public void addRental(Rental arg) {
		rentals.add(arg);
	}

	public String createStatement() {

		Report report = new Report();
		report.setCustomerName(customerName);

		for (Rental rental : getRentals()) {
			final double thisAmount = calcAmount(rental);
			report.addFrequentRenterPoints(bonusStrategy.calculateBonus(rental, this));
			report.addMovieData(rental.getMovie().getTitle(), thisAmount);
		}

		return report.toString();
	}

	private double calcAmount(Rental rental) {
		switch (rental.getMovie().getPriceCode()) {
		case REGULAR:
			return calcRegularAmount(rental);

		case NEW_RELEASE:
			return calcNewReleaseAmount(rental);

		case CHILDREN:
			return calcChildrenAmount(rental);
		}
		return 0;
	}

	private double calcChildrenAmount(Rental rental) {
		double amount = 1.5;
		final double discount = 1.5;
		if (rental.getDaysRented() > 3) {
			amount = (rental.getDaysRented() - 3) * discount;
		}
		return amount;
	}

	double calcNewReleaseAmount(Rental rental) {
		final double discount = 3;
		return rental.getDaysRented() * discount;
	}

	private double calcRegularAmount(Rental rental) {
		double amount = 2;
		final double discount = 1.5;
		if (rental.getDaysRented() > 2) {
			amount += (rental.getDaysRented() - 2) * discount;
		}
		return amount;
	}
	
	public List<Rental> getRentals() {
		return Collections.unmodifiableList(rentals);
	}

	public IBonusRecountStrategy getBonusStrategy() {
		return bonusStrategy;
	}

	public void setBonusStrategy(IBonusRecountStrategy bonusStrategy) {
		this.bonusStrategy = bonusStrategy;
	}
}
